﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;


namespace CommandLib
{
    /// <summary>
    /// 字符串扩展类
    /// </summary>
    public static class StringOption
    {
        /// <summary>
        /// 判断字符是否为空
        /// </summary>
        /// <param name="value">string</param>
        /// <returns>bool</returns>
        public static bool IsEmpty(this string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return true;
            }
            else
                return false;

        }
        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string GetString(this Stream stream, long length = 0)
        {

            if (length == 0)
            {
                length = stream.Length;
            }
            if (length > 0)
            {
                byte[] bytes = new byte[length];
                stream.Read(bytes, 0, bytes.Length);
                return Encoding.UTF8.GetString(bytes);
            }
            else
            {
                return string.Empty;
            }

        }

        /// <summary>
        /// 获取参数字符串
        /// </summary>
        /// <param name="nameValue"></param>
        /// <returns></returns>
        public static string GetString(this NameValueCollection nameValue)
        {
            var result = new StringBuilder();
            if (nameValue != null)
            {
                foreach (string name in nameValue)
                {
                    result.Append($"{name}={nameValue[name]}&");
                }
            }
            return result.ToString().TrimEnd('&');

        }
        /// <summary>
        /// 字符串反转
        /// </summary>
        /// <param name="value">字符串参数</param>
        /// <returns></returns>
        public static string Reverses(this string value)
        {
            var result = string.Empty;
            if (!string.IsNullOrEmpty(value))
            {
                var arr = value.Reverse();
                result = new string(arr.ToArray());
            }
            return result;
        }

        /// <summary>
        /// 获取字符串大小
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>返回大小值</returns>
        public static long GetSize(this string str)
        {
            if (str == null)
            {
                return 0;
            }
            if (str == string.Empty)
            {
                return 1;
            }
            return Encoding.UTF8.GetBytes(str).Length;
        }
        /// <summary>
        /// 获取字符大小
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="encoding">字符编码格式</param>
        /// <returns>返回大小值</returns>
        public static long GetSize(this string str, Encoding encoding)
        {
            if (str == null)
            {
                return 0;
            }
            if (str == string.Empty)
            {
                return 1;
            }
            return encoding.GetBytes(str).Length;
        }
        /// <summary>
        /// 获取参数
        /// </summary>
        /// <param name="nameValue">键值对</param>
        /// <returns>返回字典</returns>
        public static Dictionary<string, string> GetPairs(this NameValueCollection nameValue)
        {

            var result = new Dictionary<string, string>();
            if (nameValue != null)
            {
                foreach (string name in nameValue)
                {
                    result.Add(name, nameValue[name]);
                }
            }
            return result;

        }

        /// <summary>
        /// 获取对象真实类型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static Type GetReallyType(this object obj)
        {
            Type type = null;
            if (obj != null)
            {
                type = obj.GetType();
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                {
                    type = type.GetGenericArguments()[0];
                }
            }
            return type;
        }

        /// <summary>
        /// 获取对象真实类型
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static Type GetReallyType(this Type type)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                return type.GetGenericArguments()[0];
            }
            else
            {
                return type;
            }

        }

        /// <summary>
        /// 验证字符串是否是数字
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static bool IsNum(this string str)
        {
            var r = new Regex(@"^[+-]?\d*(,\d{3})*(\.\d+)?$");
            if (r.IsMatch(str))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 流转比特
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static byte[] ToByteArray(this Stream @this)
        {
            MemoryStream memoryStream = @this as MemoryStream;
            if (memoryStream != null)
            {
                return memoryStream.ToArray();
            }

            using MemoryStream memoryStream2 = new MemoryStream();
            @this.CopyTo(memoryStream2);
            return memoryStream2.ToArray();
        }

        /// <summary>
        /// 比特转流
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Stream BytesToStream(this byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
        /// <summary>
        /// 返回单个正则匹配值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="regex">正则表达式字符串</param>
        /// <returns>字符串类型</returns>
        public static string ToRegexString(this string value, string regex)
        {

            if (value != null)
            {
                Regex re = new Regex(regex);
                Match m = re.Match(value);
                if (m.Success)
                {
                    return m.Value;
                }
                else
                {
                    return null;
                }

            }
            else
            {
                return null;
            }

        }
        /// <summary>
        /// 返回正则匹配字符串数组
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="regex">正刚表达式</param>
        /// <returns>字符串数组</returns>
        public static string[] ToRegexStringArray(this string value, string regex)
        {
            string[] array = { };
            if (value != null)
            {
                Regex rg = new Regex(regex, RegexOptions.Multiline);
                MatchCollection mc = rg.Matches(value);
                if (mc.Count > 0)
                {
                    int group = mc.Count;
                    array = new string[group];
                    for (int i = 0; i < group; i++)
                    {
                        array[i] = mc[i].Value;
                    }
                }

            }
            return array;
        }
        /// <summary>
        /// 判断是否匹配
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="regex">正则表达式</param>
        /// <returns>bool</returns>
        public static bool IsRegex(this string value, string regex)
        {
            if (value != null)
            {
                Regex reg = new Regex(regex);
                return reg.IsMatch(value);
            }
            else
            {
                return false;
            }

        }
        /// <summary>
        /// 以字符串的方式分割字符数组
        /// </summary>
        /// <param name="str">要分割的字符</param>
        /// <param name="sp">字符串分割方式</param>
        /// <returns></returns>
        public static string[] RegexSplit(this string str, string sp)
        {
            if (str != null)
            {
                return Regex.Split(str, sp);
            }
            else
            {
                return null;
            }


        }
    }
}
